package com.loveyukari.tmp;

import com.loveyukari.douyu.utils.DouyuMessageUtil;
import com.loveyukari.utils.MD5Utils;
import net.sf.json.JSONObject;
import org.java_websocket.WebSocket;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.nio.ByteBuffer;
import java.util.HashMap;

public class TestWebSocket {

    public static void main(String[] args) throws InterruptedException {
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Origin","https://www.douyu.com");
        WebSocketClient webSocket = new WebSocketClient(URI.create("wss://wsproxy.douyu.com:6673"), new Draft_6455(),headers,0) {
            @Override
            public void onOpen(ServerHandshake serverHandshake) {
                System.out.println(serverHandshake.getHttpStatus());
                System.out.println(serverHandshake.getHttpStatusMessage());


                long time = System.currentTimeMillis()/1000;
                String did = "88788128efed0162e76be98800091501";
                String vk = MD5Utils.md5String32(time+"r5*^5;}2#\\${XF[h+;'./.Q'1;,-]f'p["+did);
                JSONObject json = new JSONObject();
                json.put("type","loginreq");
                json.put("username","51126709");
                json.put("password","");
                json.put("roomid","196");
                json.put("ltkid","77858748");
                json.put("biz","1");
                json.put("stk","a079a7423703ee9f");
                json.put("dfl","");
                json.put("ct","0");
                json.put("devid",did);
                json.put("rs",time);
                json.put("pt",2);
                json.put("vk",vk);
                json.put("ver","20180222");
                json.put("aver","20180301");
//        json.put("username","");
//        json.put("password","");
//        json.put("roomid",196);
//        json.put("ltkid","");
//        json.put("biz","");
//        json.put("stk","");
//        json.put("dfl","");
//        json.put("ct","");
//        json.put("devid",did);
//        json.put("rs",time);
//        json.put("pt",2);
//        json.put("vk",vk);
//        json.put("ver","20180222");
//        json.put("aver","20180301");
                byte[] requestData = DouyuMessageUtil.getRequestData(json);


                this.send(requestData);
            }

            @Override
            public void onMessage(String s) {
                System.out.println(s);
            }

            @Override
            public void onClose(int i, String s, boolean b) {
                System.out.println(i);
                System.out.println(s);
                System.out.println(b);
            }

            @Override
            public void onError(Exception e) {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }

            @Override
            public void onMessage(ByteBuffer bytes) {
            }
        };
        webSocket.connect();

    }


}
